package com.pig4cloud.pigx.contract.executer.trade;

import com.pig4cloud.pigx.contract.constant.ContractMqConstants;
import com.pig4cloud.pigx.contract.executer.trade.handler.TradeHandlerManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 订单执行器
 *
 * @since 2022/8/1p 10:54
 */
@Slf4j
@Component
@AllArgsConstructor
public class TradeExecutor {

	private final TradeHandlerManager tradeHandlerManager;

	/**
	 * 监听 contract.trade.new 队列，处理成交记录
	 *
	 * @param tradeId 成交记录id
	 * @since 2022/8/17 21:00
	 */
	@RabbitListener(bindings = {
			@QueueBinding(
					value = @Queue(value = ContractMqConstants.QUEUE_FANOUT_CONTRACT_TRADE_NEW),
					exchange = @Exchange(value = ContractMqConstants.EXCHANGE_FANOUT_CONTRACT_TRADE_NEW, type = ExchangeTypes.FANOUT)
			)})
	public void trade(Long tradeId) {
		log.info("{} 开始处理成交记录。id：{}", this.getClass().getSimpleName(), tradeId);

		try {
			tradeHandlerManager.handle(tradeId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 监听 contract.trade.new.robot 队列，处理机器人成交记录
	 *
	 * @param tradeId 成交记录id
	 * @since 2022/8/17 21:00
	 */
	@RabbitListener(bindings = {
			@QueueBinding(
					value = @Queue(value = ContractMqConstants.QUEUE_FANOUT_CONTRACT_TRADE_NEW_ROBOT_K_LINE),
					exchange = @Exchange(value = ContractMqConstants.EXCHANGE_FANOUT_CONTRACT_TRADE_NEW_ROBOT_K_LINE, type = ExchangeTypes.FANOUT)
			)})
	public void tradeRobot(Long tradeId) {
		log.info("{} 开始处理成交记录。id：{}", this.getClass().getSimpleName(), tradeId);

		try {
			tradeHandlerManager.handle(tradeId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
